int main()
{
int fd = -1;
int adc_value;
char buff[64] = {0};
// Open the ADC node file (ADC0 corresponds to PMU_GPIO6)
fd = open("/sys/bus/iio/devices/iio:device1/in_voltage_adc_gpio6_input", O_RDONLY);
if(fd < 0){
perror("open error\n");
return fd;
}
// Read the ADC value int n_read = read(fd, buff, sizeof(buff));
if(n_read < 0) {perror("read error\n");close(fd);return -1;}// Convert the read string into an integer (unit: μV)
adc_value = atol(buff);
printf("read %d bytes, content: %s\n", n_read, buff);
printf("ADC value: %d μV\n", adc_value);
close(fd);
return 0;
}
```
**Code Description:**
- Use `open ()` function to open the ADC node file `/sys/bus/iio/devices/iio: device0/in_voltage_adc_gpio4_input`.
- Use `read ()` function to read the raw ADC value (in string format, unit: μV).
- Use the `atol ()` function to convert a string to a long integer value.
- The unit of the read ADC value is microvolts (μV), which can be converted into a voltage value as required.
**Compile and run:**
```bash
# Compile with NDK # Push the compiled executable file to the device and run it
adb push test_adc /data/local/tmp/test_adc
adb shell "chmod +x /data/local/tmp/test_adc"
adb shell "/data/local/tmp/test_adc"
```
### Test Data
#### ADC Pin Specifications
The ADC pin definitions on the module are as follows:
|
Interface
|
Module Pin Number
|
Module Pin Name
|
Module I/O
|
NET Name (Multiplexing Function)
|
Description
|
| --- | --- | --- | --- | --- | --- |
|
ADC
|
128
|
ADC0
|
I
|
ADC0
|
1) Connector input voltage range: 0–5.25 V.
2) Voltage dividing resistors: RH = 18 kΩ, RL = 10 kΩ.
3) ADC pin voltage = connector input voltage (J0403) ×(10/28).
4) Example: When the input is 3 V, the voltage at the ADC pin shall be 3 V × 10/28 ≈ 1.0714 V.
|
|
ADC
|
185
|
ADC1
|
I
|
ADC1
|
Same electrical characteristics as ADC0 (0–5.25 V input range, RH = 18 kΩ, RL = 10 kΩ, ADC pin voltage = Vin × 10/28).
|
#### ADC0 Test Data
|
Voltage Source
|
ADC0(μV)
|
Theoretical Value (V)
|
| --- | --- | --- |
|
1.8 V
| 638408 | 1.8 V×10/28 = 0.642857 |
|
3 V
| 1066414 | 3 V×10/28 = 1.07143 |
|
5 V
| 1784623 | 5 V×10/28 = 1.78571 |
#### ADC1 Test Data
|
Voltage Source
|
ADC1(μV)
|
Theoretical Value (V)
|
| --- | --- | --- |
|
1.8 V
| 640549 | 1.8 V×10/28 = 0.642857 |
|
3 V
| 1069269 | 3 V×10/28 = 1.07143 |
|
5 V
| 1789814 | 5 V×10/28 = 1.78571 |